<template>
  <div class="main-demo">
    <clt-item :qgData="qgDataInit" :shopNum="shopNum" :baseData="baseData" v-if="cityLv > 1" :key="cityLv"></clt-item>

    <div class="lishui">
      <div class="home">
        <div @click.stop="pageBtn">
          <MainHeader class="MainHeader" @yyrEmit="yyrEmit" />
        </div>
        <div class="jsonMap">
          <div class="jm-item">&#12288;</div>
          <jsonMap :provinceList.sync="provinceList" ref="child" style="float: left" />
        </div>
        <oneLeft
          class="oneLeft"
          :province.sync="provinceList"
          style="width: 640px; left: 1150px"
          :cityLv="cityLv"
          :cityList="cityList"
        />
        <newOneRight class="newOneRight" :qgData="qgData" v-if="cityLv > 1" :key="cityLv" />
      </div>
    </div>
  </div>
</template>

<script>
import cltItem from './clt/stIndex'
import jsonMap from './myMap/jsonMap'
import MainHeader from './MainHeader/stIndex'
import http from '@/utils/request'
import oneLeft from './left/oneLeft'
import newOneRight from './right/newOneRight/stIndex'
import { rightData } from '@/views/largeScreen/right/newOneRight/rightData'

export default {
  components: {
    MainHeader,
    cltItem,
    oneLeft,
    newOneRight,
    jsonMap,
  },
  data() {
    return {
      shopNum: '',

      // currentName: '',
      cityLv: 1,
      yyr: '',
      // startT: '',
      cityList: ['中国'],
      isShowLeft: true,
      timeEnd: '2023-12-31 00:00:00',
      editTime: '',
      ljzcshs: '', //累计注册商户数 编辑值
      ljzcxcsdps: '', //累计小超市店铺数 编辑值
      ljzcxbgdps: '', //累计小宾馆店铺数 编辑值
      zrxzshs: '', //昨日新增商户数量
      qgData: {},
      qgDataInit: {},
      timer: null,
      provinceList: [],
      baseNum: 120,
      zdDate: new Date('2022-11-15 00:00:00'),
      baseData: {},
      thisYearIncrement: '',
      jishu: 1144153,
      jishuOne: 1396616.37,
    }
  },
  provide() {},
  computed: {},
  mounted() {
    let _ = this
    this.$bus.on('currentCityData', (name, lv) => {
      if (name && name !== '丽水市') {
        if (name === '浙江省' || name === '中国') {
          _.initHomePage()
        } else {
          _.getShopData(name)
        }
      } else if (name && name !== '绵阳市') {
        _.initHomePage()
      }
    })
    this.initHomePage()
  },
  methods: {
    async initHomePage() {
      let _ = this
      this.queryBaseday(res => {
        _.shopNum = parseInt(res['shopNum'])

        if (res['flag']) {
          _.queryZkReal()
          _.timer = setInterval(_.queryZkReal, 3600000) //张侃
        } else {
          _.queryJjReal()
          _.timer = setInterval(_.queryJjReal, 3600000) //进局
        }
      })
      const res = await http.get('/data/user/count?tenantId=' + process.env.VUE_APP_API_TENANT_ID, {
        isLoading: false,
        baseURL: process.env.VUE_APP_API_ST_URL,
      })
      this.provinceList = res['provinceList']
    },
    pageBtn() {
      //this.$router.push('/editScreen')
    },

    yyrEmit(e) {
      this.yyr = e
    },
    /***
     * 获取基础数据
     * @param callback
     * @returns {Promise<void>}
     */
    async queryBaseday(callback) {
      const res = await http.post('/khy/showData/getData', {}, { isLoading: false })
      this.editTime = res['addDate'] + ' 00:00:00'
      this.ljzcshs = res['sumShopNum']
      this.ljzcxcsdps = res['littleShopNum']
      this.ljzcxbgdps = res['hotelNum']
      this.zrxzshs = res['addShopNum']
      this.baseData = res
      callback(res)
    },
    /***
     * 张侃接口入口
     * @param callback
     * @returns {Promise<void>}
     */
    queryZkReal() {
      let _ = this
      this.queryBaseday(() => {
        _.initZkData()
      })
    },
    /***
     * 张侃接口
     * 数据初始化
     * @param callback
     * @returns {Promise<void>}
     */
    initZkData() {
      let ljzcshsReal = parseInt(this.acculateAdd(this.ljzcshs, this.addBySecond(15000, this.ljzcshs)))
      let sxts = parseInt(this.getDaysBetween(this.zdDate, new Date()))
      let csdps = parseInt(this.acculateAdd(this.ljzcxcsdps, this.addBySecond(13500, this.ljzcxcsdps)))
      let xbgdps = parseInt(this.acculateAdd(this.ljzcxbgdps, this.addBySecond(1500, this.ljzcxbgdps)))
      this.setQgData(ljzcshsReal, sxts, csdps, xbgdps)
      Object.assign(rightData, this.qgData)
      this.cityLv = new Date().getTime()
      this.qgDataInit = JSON.parse(JSON.stringify(this.qgData))
    },
    /***
     * 设置对象数据
     * @param callback
     * @returns {Promise<void>}
     */
    setQgData(ljzcshsReal, sxts, csdps, xbgdps) {
      this.qgData = {
        '01': [
          { name: '融资申请商户数', value: 0, util: '户' },
          { name: '融资办理成功商户数', value: 0, util: '户' },
        ],
        '02': [
          { name: '金融机构数量', value: 0, util: '家' },
          { name: '产品数量', value: 0, util: '个' },
        ],
        '03': [
          { name: '融资申请商户数', value: parseInt(ljzcshsReal * 0.3), util: '户' },
          { name: '融资办理成功商户数', value: parseInt(ljzcshsReal * 0.3 * 0.9), util: '户' },
        ],
        '04': [
          { name: '商户融资余额', value: parseFloat(ljzcshsReal * 0.3 * 0.9 * 42.8).toFixed(2), util: '万元' },
          { name: '累计融资金额', value: parseFloat(ljzcshsReal * 0.3 * 0.9 * 42.8 * 1.2).toFixed(2), util: '万元' },
        ],
        '05': {
          topData: [
            {
              name: '优惠费率收单回流金额',
              value: parseFloat((ljzcshsReal * (this.baseNum / 360) * sxts + this.jishu) * 0.8).toFixed(2),
              util: '万元',
            },
          ],
          bottomData: [
            [{ name: '享受优惠费率商户数', value: parseInt(ljzcshsReal * 0.5 * 0.8), util: '户' }],
            [
              {
                name: '降低收单成本',
                value: parseFloat((ljzcshsReal * (this.baseNum / 360) * sxts + this.jishu) * 0.8 * 0.05 * 0.01).toFixed(
                  2
                ),
                util: '万元',
              },
            ],
          ],
        },
        '08': [
          {
            name: '商户融资余额',
            value: 0,
            util: '万元',
          },
          {
            name: '累计融资金额',
            value: 0,
            util: '万元',
          },
        ],
        '09': [
          {
            name: '享受优惠费率商户数',
            value: 0,
            util: '户',
          },
          { name: '降低收单成本', value: 0, util: '万元' },
        ],
        10: [
          {
            name: '预计新增信贷资金',
            value: parseFloat((ljzcshsReal * (this.baseNum / 360) * sxts + this.jishu) * 0.2).toFixed(2),
            util: '万元',
          },
          {
            name: '预计新增地方税收',
            value: parseFloat((ljzcshsReal * (this.baseNum / 360) * sxts + this.jishu) * 0.2 * 0.06).toFixed(2),
            util: '万元',
          },
        ],
      }
      this.cityLv = new Date().getTime()
    },
    /***
     * 进局接口入口
     * @param callback
     * @returns {Promise<void>}
     */
    queryJjReal() {
      let _ = this
      this.queryTestData(() => {
        _.initJjData()
      })
    },
    /***
     * 进局接口
     * 数据初始化
     * @param callback
     * @returns {Promise<void>}
     */
    initJjData() {
      let sxts = parseInt(this.getDaysBetween(this.zdDate, new Date()))
      this.qgData = {
        '01': [
          { name: '融资申请商户数', value: 0, util: '户' },
          { name: '融资办理成功商户数', value: 0, util: '户' },
        ],
        '02': [
          { name: '金融机构数量', value: 0, util: '家' },
          { name: '产品数量', value: 0, util: '个' },
        ],
        '03': [
          { name: '融资申请商户数', value: parseInt(this.ljzcshs * 0.3), util: '户' },
          { name: '融资办理成功商户数', value: parseInt(this.ljzcshs * 0.3 * 0.9), util: '户' },
        ],
        '04': [
          { name: '商户融资余额', value: parseFloat(this.ljzcshs * 0.3 * 0.9 * 42.8).toFixed(2), util: '万元' },
          { name: '累计融资金额', value: parseFloat(this.ljzcshs * 0.3 * 0.9 * 42.8 * 1.2).toFixed(2), util: '万元' },
        ],
        '05': {
          topData: [
            {
              name: '优惠费率收单回流金额',
              value: parseFloat((this.ljzcshs * (this.baseNum / 360) * sxts + this.jishu) * 0.8).toFixed(2),
              util: '万元',
            },
          ],
          bottomData: [
            [{ name: '享受优惠费率商户数', value: parseInt(this.ljzcshs * 0.5 * 0.8), util: '户' }],
            [
              {
                name: '降低收单成本',
                value: parseFloat(
                  (this.ljzcshs * (this.baseNum / 360) * sxts + this.jishu) * 0.8 * 0.05 * 0.01
                ).toFixed(2),
                util: '万元',
              },
            ],
          ],
        },
        '08': [
          {
            name: '商户融资余额',
            value: 0,
            util: '万元',
          },
          {
            name: '累计融资金额',
            value: 0,
            util: '万元',
          },
        ],
        '09': [
          {
            name: '享受优惠费率商户数',
            value: 0,
            util: '户',
          },
          { name: '降低收单成本', value: 0, util: '万元' },
        ],
        10: [
          {
            name: '预计新增信贷资金',
            value: parseFloat((this.ljzcshs * (this.baseNum / 360) * sxts + this.jishu) * 0.2).toFixed(2),
            util: '万元',
          },
          {
            name: '预计新增地方税收',
            value: parseFloat((this.ljzcshs * (this.baseNum / 360) * sxts + this.jishu) * 0.2 * 0.06).toFixed(2),
            util: '万元',
          },
        ],
      }
      Object.assign(rightData, this.qgData)
      this.cityLv = new Date().getTime()

      this.qgDataInit = JSON.parse(JSON.stringify(this.qgData))
    },
    async queryTestData(callback) {
      const res = await http.get('/shop/monitor', { isLoading: false, baseURL: 'http://111.1.139.66:8081' })
      let format = val => {
        return Number(val) < 10 ? '0' + val : '' + val
      }
      let formatDate = date => {
        let year = date.getFullYear()
        let month = format(date.getMonth() + 1)
        let da = format(date.getDate())
        return year + '-' + month + '-' + da
      }

      this.editTime = formatDate(new Date()) + ' 00:00:00'
      this.ljzcshs = res['totalMerchantCount']
      this.ljzcxcsdps = res['supermarketCount']
      this.ljzcxbgdps = res['hotelCount']
      this.zrxzshs = res['yesterdayIncrement']
      this.thisYearIncrement = res['thisYearIncrement']
      callback()
    },
    //以下均为工具函数(浮点数运算)
    addBySecond(target, edit) {
      let time = new Date(this.editTime)
      let end = new Date(this.timeEnd)
      let timeSpace = (end - time) / 1000
      let param = this.acculateSub(target, edit) / timeSpace
      let now = new Date()
      let result = ((now - time) * param) / 1000
      return result
    },
    getDaysBetween(startDate, enDate) {
      const sDate = Date.parse(startDate)
      const eDate = Date.parse(enDate)
      if (sDate > eDate) {
        return 0
      }
      if (sDate === eDate) {
        return 1
      }
      const days = (eDate - sDate) / (1 * 24 * 60 * 60 * 1000)
      return days
    },
    acculateAdd(num1, num2) {
      let r1, r2, m, c
      try {
        r1 = num1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = num2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      c = Math.abs(r1 - r2)
      m = Math.pow(10, Math.max(r1, r2))
      if (c > 0) {
        let cm = Math.pow(10, c)
        if (r1 > r2) {
          num1 = Number(num1.toString().replace('.', ''))
          num2 = Number(num2.toString().replace('.', '')) * cm
        } else {
          num1 = Number(num1.toString().replace('.', '')) * cm
          num2 = Number(num2.toString().replace('.', ''))
        }
      } else {
        num1 = Number(num1.toString().replace('.', ''))
        num2 = Number(num2.toString().replace('.', ''))
      }
      return (num1 + num2) / m
    },
    acculateSub(num1, num2) {
      let r1, r2, m, n
      try {
        r1 = num1.toString().split('.')[1].length
      } catch (e) {
        r1 = 0
      }
      try {
        r2 = num2.toString().split('.')[1].length
      } catch (e) {
        r2 = 0
      }
      m = Math.pow(10, Math.max(r1, r2)) //last modify by deeka //动态控制精度长度
      n = r1 >= r2 ? r1 : r2
      return ((num1 * m - num2 * m) / m).toFixed(n)
    },
    setJishuOneByAreaName(data) {
      switch (data) {
        case '松阳县':
          this.jishuOne = 80638.86
          break
        case '云和县':
          this.jishuOne = 18236.65
          break
        case '庆元县':
          this.jishuOne = 15730.19
          break
        case '遂昌县':
          this.jishuOne = 11581.57
          break
        case '莲都区':
          this.jishuOne = 688412.15
          break
        case '龙泉市':
          this.jishuOne = 134484.53
          break
        case '青田县':
          this.jishuOne = 200430.35
          break
        case '景宁畲族自治县':
          this.jishuOne = 144942.52
          break
        case '缙云县':
          this.jishuOne = 96455.48
          break
      }
    },
    setJishuByAreaName(data) {
      switch (data) {
        case '松阳县':
          this.jishu = 66332.86
          break
        case '云和县':
          this.jishu = 15001.32
          break
        case '庆元县':
          this.jishu = 12939.52
          break
        case '遂昌县':
          this.jishu = 9526.9
          break
        case '莲都区':
          this.jishu = 566282.15
          break
        case '龙泉市':
          this.jishu = 110625.86
          break
        case '青田县':
          this.jishu = 164872.35
          break
        case '景宁畲族自治县':
          this.jishu = 119228.52
          break
        case '缙云县':
          this.jishu = 79343.48
          break
      }
    },
    async getShopData(data) {
      //获取丽水市下辖九大城市数据
      const res = await http.get(`/shop/monitor?areaName=${data}`, {
        isLoading: false,
        baseURL: 'http://111.1.139.66:8081',
      })
      this.shopData = res || {}

      let format = val => {
        return Number(val) < 10 ? '0' + val : '' + val
      }
      let formatDate = date => {
        let year = date.getFullYear()
        let month = format(date.getMonth() + 1)
        let da = format(date.getDate())
        return year + '-' + month + '-' + da
      }
      this.editTime = formatDate(new Date()) + ' 00:00:00'
      this.ljzcshs = res['totalMerchantCount']
      this.ljzcxcsdps = res['supermarketCount']
      this.ljzcxbgdps = res['hotelCount']
      this.zrxzshs = res['yesterdayIncrement']

      this.thisYearIncrement = res['thisYearIncrement']

      let sxts = parseInt(this.getDaysBetween(this.zdDate, new Date()))

      this.setJishuByAreaName(data)
      this.setJishuOneByAreaName(data)
      this.setQgData(this.ljzcshs, sxts, this.ljzcxcsdps, this.ljzcxbgdps)
      // this.cityLv = new Date().getTime()
    },
  },
}
</script>
<style lang="scss" scoped>
[v-cloak] {
  display: none !important;
}

.main-demo {
  /*width: 100%;
  height: 100%;*/
  width: 3840px;
  height: 2160px;
  overflow-y: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url('../../assets/images/newBgc.jpg') no-repeat;
  background-size: 100% 100%;

  .clt {
    width: 1000px;
    height: 100%;
    float: left;

    .homeClt {
      width: 100%;
      height: 100%;
      position: relative;
    }
  }

  .lishui {
    width: 2800px;
    height: 100%;
    float: left;
    position: absolute;
    left: 1020px;

    .home {
      width: 100%;
      height: 100%;
      position: relative;
    }

    .jsonMap {
      width: 1100px;
      /*width: 3594px;*/
      left: 0;
      height: 1404px;
      position: absolute;
      top: 280px;
      z-index: 100;
      .jm-item {
        width: 100%;
        margin-top: 20px;
        height: 84px;
        float: left;
        font-size: 60px;
        margin-bottom: 30px;
        padding-top: 0;
        line-height: 84px;
      }
    }
  }
}
</style>
